package com.education.framework.session;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.education.domain.LoginUser;
import com.education.domain.User;
import com.education.framework.constants.ConstantsSession;

public class SessionHelper implements Serializable{

	private static final long serialVersionUID = 1L;

	private static SessionHelper sessionHelper = null;
	
	private HttpSession session;
	
	private SessionHelper(){
	}
	
	public synchronized static SessionHelper getInstance(){
		if(null == sessionHelper){
			sessionHelper = new SessionHelper();
		}
		return sessionHelper;
	}
	
	public LoginUser getLoginUser(){
		Object obj = getSession().getAttribute(ConstantsSession.LOGIN_STU_TEA);
		if(null != obj){
			return (LoginUser)obj;
		}
		return null;
	}
	public void putLoginUser(LoginUser loginUser){
		getSession().setAttribute(ConstantsSession.LOGIN_STU_TEA, loginUser);
	}
	public void clearLoginUser(){
		getSession().removeAttribute(ConstantsSession.LOGIN_STU_TEA);
	}
	
	public User getUser(){
		Object obj = getSession().getAttribute(ConstantsSession.LOGIN_USER);
		if(null != obj){
			return (User)obj;
		}
		return null;
	}
	public void putUser(User user){
		getSession().setAttribute(ConstantsSession.LOGIN_USER, user);
	}

	public HttpSession getSession() {
		HttpServletRequest request = (HttpServletRequest) ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		session = request.getSession(true);
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}
	
}
